package com.hyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hyz.model.IsComplete;
import com.hyz.model.ResponseResult;
import com.hyz.model.assetMaintenance.AssetMaintenance;
import com.hyz.model.assetMaintenance.AssetMaintenanceResult;
import com.hyz.model.assetRetirement.AssetRetirement;
import com.hyz.model.assetRetirement.AssetRetirementResult;
import com.hyz.model.assetSecondment.AssetSecondment;
import com.hyz.model.assetSecondment.AssetSecondmentResult;
import com.hyz.model.assetSecondment.AssetSecondmentStatus;
import com.hyz.service.AssetMaintenanceService;
import com.hyz.service.AssetRetirementService;
import com.hyz.service.AssetSecondmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/10 15:11
 */
@Api("运维人员")
@RestController
@RequestMapping("/operations")
public class OperationsController {
    @Autowired
    private AssetSecondmentService asService;

    //------------------------------------------------------------------------------------------------
    // 资产借调

    @ApiOperation("运维人员获取资产借调申请")
    @PostMapping("/opGetAssetSecondments")
    public ResponseResult<List<AssetSecondment>> opGetAssetSecondments() {
        QueryWrapper<AssetSecondment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .in(AssetSecondment::getResult,
                        AssetSecondmentResult.APPROVAL_AGREE,
                        AssetSecondmentResult.FINANCIAL_AGREE);
        List<AssetSecondment> assetSecondments = asService.list(wrapper);
        if (assetSecondments.isEmpty()) {
            return new ResponseResult<>(400, "借调申请为空");
        }
        return new ResponseResult<>(200, "资产借调申请", assetSecondments);
    }

    @ApiOperation("运维人员审批资产借调申请")
    @PostMapping("/opUpdateAssetSecondments")
    public ResponseResult<String> opUpdateAssetSecondments(@RequestParam("assetSecondmentId") Long assetSecondmentId,
                                                           @RequestParam("result") AssetSecondmentResult result) {
        // 1. 判断对应的申请是否存在
        AssetSecondment as = asService.getById(assetSecondmentId);
        if (Objects.isNull(as)) {
            return new ResponseResult<>(400, "对应申请不存在");
        }

        // 2. 判断对应申请是否为 已完成 且资产状态为 部门经理同意 或 财务人员同意
        if (Objects.equals(as.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "申请已完成，请刷新");
        }

        // 4. 判断对应的申请结果为 0
        boolean borrow = Objects.equals(as.getStatus(), AssetSecondmentStatus.BORROW) && Objects.equals(as.getResult(), AssetSecondmentResult.APPROVAL_AGREE);
        boolean transfer = Objects.equals(as.getStatus(), AssetSecondmentStatus.TRANSFERS) && Objects.equals(as.getResult(), AssetSecondmentResult.FINANCIAL_AGREE);
        if (!borrow && !transfer) {
            return new ResponseResult<>(400, "资产借调申请状态不符合");
        }

        // 5. 对申请结果进行修改
        IsComplete is = IsComplete.UNFINISHED;
        if (Objects.equals(result, AssetSecondmentResult.FAILED)) {
            is = IsComplete.COMPLETE;
        }
        UpdateWrapper<AssetSecondment> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(AssetSecondment::getResult, result)
                .set(AssetSecondment::getIsComplete, is)
                .eq(AssetSecondment::getId, assetSecondmentId)
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .in(AssetSecondment::getResult, AssetSecondmentResult.APPROVAL_AGREE, AssetSecondmentResult.FINANCIAL_AGREE);
        if (asService.update(wrapper)) {
            // 7. 返回所有本部门的资产借用申请
            return new ResponseResult<>(200, "审批成功");
        }
        return new ResponseResult<>(400, "审批错误，请重新选择");
    }

    //------------------------------------------------------------------------------------------------
    // 资产维修

    @Autowired
    private AssetMaintenanceService amService;

    @ApiOperation("运维人员获取维修申请")
    @PostMapping("opGetAssetMaintenance")
    public ResponseResult<List<AssetMaintenance>> opGetAssetMaintenance() {
        QueryWrapper<AssetMaintenance> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetMaintenance::getIsComplete, IsComplete.UNFINISHED)
                .eq(AssetMaintenance::getResult, AssetMaintenanceResult.CREATE);
        List<AssetMaintenance> list = amService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "维修申请为空");
        }
        return new ResponseResult<>(200, "维修申请", list);
    }

    @ApiOperation("运维人员审批维修申请")
    @PostMapping("opUpdateAssetMaintenance")
    public ResponseResult<Boolean> opUpdateAssetMaintenance(@RequestParam("assetMaintenanceId") Long assetMaintenanceId,
                                                            @RequestParam("result") AssetMaintenanceResult result) {
        // 1. 判断申请是否存在
        AssetMaintenance am = amService.getById(assetMaintenanceId);
        if (Objects.isNull(am)) {
            return new ResponseResult<>(400, "维修申请不存在", false);
        }
        // 2. 判断申请是否完成
        if (Objects.equals(am.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "维修申请已完成", false);
        }
        // 3. 判断申请状态
        if (!Objects.equals(am.getResult(), AssetMaintenanceResult.CREATE)) {
            return new ResponseResult<>(400, "维修申请状态异常，请重试", false);
        }
        // 4. 判断结果是否为维修成功.根据结果生成不同的申请状态
        if (Objects.equals(result, AssetMaintenanceResult.OPERATIONS_SUCCESS)) {
            // 运维人员维修成功
            am.setIsComplete(IsComplete.COMPLETE);
        }
        am.setResult(result);
        if (amService.saveOrUpdate(am)) {
            return new ResponseResult<>(200, "维修申请审批成功", true);
        }
        return new ResponseResult<>(400, "维修申请审批失败", false);
    }

    //------------------------------------------------------------------------------------------------
    // 资产报废

    @Autowired
    private AssetRetirementService arService;

    @ApiOperation("维修人员获取报废申请")
    @PostMapping("opGetAssetRetirement")
    public ResponseResult<List<AssetRetirement>> opGetAssetRetirement() {
        QueryWrapper<AssetRetirement> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AssetRetirement::getResult, AssetRetirementResult.CREATE)
                .eq(AssetRetirement::getIsComplete, IsComplete.UNFINISHED);
        List<AssetRetirement> list = arService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无报废申请");
        }
        return new ResponseResult<>(200, "报废申请", list);
    }

    @ApiOperation("维修人员审批报废申请")
    @PostMapping("opUpdateAssetRetirement")
    public ResponseResult<Boolean> opUpdateAssetRetirement(@RequestParam("assetRetirementId") Long assetRetirementId,
                                                           @RequestParam("result") AssetRetirementResult result) {
        // 1. 判断申请是否存在
        AssetRetirement ar = arService.getById(assetRetirementId);
        if (Objects.isNull(ar)) {
            return new ResponseResult<>(400, "申请不存在", false);
        }
        // 2. 判断申请是否完成
        if (Objects.equals(ar.getIsComplete(), IsComplete.COMPLETE)) {
            return new ResponseResult<>(400, "申请已完成", false);
        }
        // 3. 判断申请的状态
        if (!Objects.equals(ar.getResult(), AssetRetirementResult.CREATE)) {
            return new ResponseResult<>(400, "申请状态不符", false);
        }
        // 4. 判断结果是否为审批失败,根据结果不同生成不同的申请状态
        if (Objects.equals(ar.getResult(), AssetRetirementResult.FAILED)) {
            ar.setIsComplete(IsComplete.COMPLETE);
        }
        ar.setResult(result);
        if (arService.updateById(ar)) {
            return new ResponseResult<>(200, "审批通过", true);
        }
        return new ResponseResult<>(400, "审批不通过", false);
    }
}
